package com.qd.panda.card;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdqidi.dto.ApiResult;
import com.cdqidi.dto.ApiResultWrapper;
import com.cdqidi.exception.ApiException;
import com.cdqidi.util.page.PageUtil;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.qd.common.panda.constant.PandaConstant;
import com.qd.common.panda.domain.entity.card.UserCardDTO;
import com.qd.common.panda.domain.entity.card.UserCardQueryDTO;
import com.qd.common.sys.enums.RemoteEnum;
import com.qd.common.sys.util.SecurityUtils;
import com.qd.panda.service.card.CardPlanHolderService;
import com.qd.panda.service.card.UserCardHolderService;
import com.qd.panda.service.card.UserCardService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotBlank;
import java.util.Optional;

/**
 * /优惠券/APP_我的优惠劵
 *
 * @author sjk
 */
@RequiredArgsConstructor
@Validated
@RestController
@RequestMapping("/app/userCard/")
@Slf4j
public class AppUserCardController {
    private final UserCardService userCardService;
    private final UserCardHolderService userCardHolderService;
    private final CardPlanHolderService cardPlanHolderService;

    /**
     * 兑换优惠券
     *
     * @param redeemCode 兑换码
     * @return 页面对象
     */
    @GetMapping(value = "/redeemVoucher")
    public ApiResultWrapper<Object> redeemVoucher(@Validated @NotBlank(message = "兑换码不能为空") String redeemCode) {
        String userId = SecurityUtils.getLoginUser().getUserId();
        String remote = SecurityUtils.getLoginUser().getRemote();
        if (!remote.equals(RemoteEnum.MINI_PROGRAM.getCode())) {
            throw new ApiException("当前用户没有权限访问");
        }
        String mobile = SecurityUtils.getLoginUser().getUser().getAppUserExtend().getMobile();
        cardPlanHolderService.redeemVoucher(userId, mobile, redeemCode);
        return ApiResult.success();
    }

    /**
     * 优惠劵详情
     *
     * @param id 主键
     * @return 记录
     */
    @GetMapping(value = "/getById")
    public UserCardDTO getById(@Validated @NotBlank(message = "ID不能为空") String id) {
        return userCardHolderService.getByIdDto(id);
    }

    /**
     * 优惠劵列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/page")
    public IPage<UserCardDTO> page(UserCardQueryDTO dto) {
        dto.setUserId(SecurityUtils.getLoginUser().getUserId());
        return userCardHolderService.page(PageUtil.buildPage(dto), dto);
    }

    /**
     * 可用优惠劵列表
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/list")
    public ObjectNode list(UserCardQueryDTO dto) {
        dto.setUserId(SecurityUtils.getLoginUser().getUserId());
        dto.setState(PandaConstant.USE_STATE_1);
        if (!StringUtils.hasLength(dto.getPrice())) {
            throw new ApiException("金额不能为空");
        }
        if (!StringUtils.hasLength(dto.getCardType())) {
            throw new ApiException("优惠卷类型不能为空");
        }
        if (!StringUtils.hasLength(dto.getUseBrand())) {
            throw new ApiException("使用品牌不能为空");
        }

        return userCardHolderService.getList(dto);
    }

    /**
     * 可用优惠劵数量
     *
     * @param dto 查询参数以及页数和每页显示条数
     * @return 列表信息
     */
    @GetMapping(value = "/availableSize")
    public ApiResultWrapper<Long> availableSize(UserCardQueryDTO dto) {
        dto = Optional.ofNullable(dto).orElse(new UserCardQueryDTO());
        dto.setUserId(SecurityUtils.getLoginUser().getUserId());
        return ApiResult.success(userCardService.availableSize(dto));
    }
}